package com.culture.config.service.impl;

import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import com.culture.config.common.util.BeanUtil;
import com.culture.config.dao.entity.UserRole;
import com.culture.config.dao.mapper.UserRoleMapper;
import com.culture.config.service.UserRoleService;
import com.culture.config.model.po.*;
import com.culture.config.model.vo.*;
import com.culture.config.wrapper.UserRoleWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class UserRoleServiceImpl implements UserRoleService{
    @Resource
    private UserRoleMapper userRoleMapper;
    
    @Override
    public Page<UserRoleVO> page(UserRolePageParam param) {
        log.info("page:{}",param);
        PageMethod.startPage(param.getPageNum(),param.getPageSize());
		Page<UserRole> userRoleList = this.userRoleMapper.page(UserRoleWrapper.wrapUserRoleQuery(param));      
		return BeanUtil.copyPageProperties(userRoleList, UserRoleVO.class);
    }

    @Override
    public List<UserRoleVO> listByUserId(Long userId) {
        List<UserRole> userRoleList = this.userRoleMapper.listByUserId(new UserRole(userId));
        if(CollectionUtils.isEmpty(userRoleList)){
            return Collections.emptyList();
        }
        return BeanUtil.copyListProperties(userRoleList, UserRoleVO.class);
    }

    @Override
    @Transactional
    public void update(UserRoleUpdatePO userRoleUpdatePO) {
        log.info("update:{}",userRoleUpdatePO);
        this.userRoleMapper.delByUserId(userRoleUpdatePO.getUserId());
        List<UserRole> addUserRoles = UserRoleWrapper.wrapUserRoleListAdd(userRoleUpdatePO);
        addUserRoles.forEach(e->this.userRoleMapper.add(e));
    }
    
}
